//
//  ZhiweiDetailController.m
//  Ten
//
//  Created by 韦显光 on 2018/8/26.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "ZhiweiDetailController.h"
#import "HomeTbvCell2.h"
#import<WebKit/WebKit.h>

@interface ZhiweiDetailController ()<WKNavigationDelegate>
{
    NSArray*dataary;
    NSDictionary*jcompanydic;
     WKWebView * wkwebview1;
    NSString*share_url;
   
}
@property (weak, nonatomic) IBOutlet UITableView *tbv;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UILabel *lb2;
@property (weak, nonatomic) IBOutlet UILabel *lb3;
@property (weak, nonatomic) IBOutlet UILabel *lb4;
@property (weak, nonatomic) IBOutlet UILabel *lb5;
@property (weak, nonatomic) IBOutlet UILabel *lb6;
@property (weak, nonatomic) IBOutlet UILabel *lb7;

@property (weak, nonatomic) IBOutlet UILabel *lb9;
@property (weak, nonatomic) IBOutlet UILabel *lb10;
@property (weak, nonatomic) IBOutlet UIImageView *logoimg;
@property (weak, nonatomic) IBOutlet UILabel *lb11;
@property (weak, nonatomic) IBOutlet UILabel *lb12;
@property (weak, nonatomic) IBOutlet UIButton *soucangBtno;

@property (weak, nonatomic) IBOutlet UIView *webback;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webbackH;


@end

@implementation ZhiweiDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loaddat];
    _tbv.separatorColor=[UIColor clearColor];
    
    //加载职位详情HTML
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    wkwebview1 = [[WKWebView alloc] initWithFrame:CGRectMake(0, 30, DEVW, 1) configuration:wkWebConfig];
    wkwebview1.scrollView.scrollEnabled = NO;
    wkwebview1.navigationDelegate = self;
    [_webback addSubview:wkwebview1];

}

-(void)loaddat{
    [FuncPublic getData:self parameters:@{@"job_id":_Xid} RequestUrl:XjobDetail success:^(NSDictionary *acountInfo) {
        dataary=acountInfo[@"like_jobs"];
        [_tbv reloadData];
        share_url=WXGStr(acountInfo[@"share_url"]);
        _soucangBtno.selected=[WXGStr(acountInfo[@"is_collect"]) isEqualToString:@"0"]?NO:YES;
        NSDictionary*jobdic=acountInfo[@"job"];
        _lb1.text=WXGStr(jobdic[@"title"]);
        _lb2.text=@"派单中";
        _lb3.text=[NSString stringWithFormat:@"%@/%@",jobdic[@"salary"],[WXGStr(jobdic[@"salary_type"])isEqualToString:@"2"]?@"月":@"日"];
        _lb4.text=[WXGStr(jobdic[@"area_name"]) empty]?@"全国":WXGStr(jobdic[@"area_name"]) ;
         _lb5.text=[NSString stringWithFormat:@"年限%@",jobdic[@"work_year"]];
         _lb6.text=[NSString stringWithFormat:@"%@以上",jobdic[@"education"]];
        _lb7.text=[NSString stringWithFormat:@"开始时间%@-结束时间%@",[FuncPublic time:jobdic[@"add_time"]],@""];
        jcompanydic=acountInfo[@"company"];
        _lb9.text=WXGStr(jobdic[@"city_area_name"]);
        _lb10.text=WXGStr(jobdic[@"address"]);
        _lb11.text=WXGStr(jcompanydic[@"name"]);
        [_logoimg sd_setImageWithURL:[jcompanydic[@"logo"]formatUrl] placeholderImage:WXGImage(@"zhanwei")];
        _lb12.text=[NSString stringWithFormat:@"%@公司规模%@人",jcompanydic[@"address"],jcompanydic[@"company_size"]];
        
        [wkwebview1 loadHTMLString:WXGStr(jobdic[@"introduce"])  baseURL:nil];
    }];
}

//d延长0.1秒算高度 防抖动
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self performSelector:@selector(loadHeight) withObject:self afterDelay:0.1];
}

- (void)loadHeight{
    CGFloat heig1 = 0.0;
    [wkwebview1 sizeToFit];
    heig1 = wkwebview1.scrollView.contentSize.height;
    _webbackH.constant=heig1+50;
    CGRect webFrame = wkwebview1.frame;
    webFrame.size.height = heig1;
    wkwebview1.frame = webFrame;
    UIView*newview=_tbv.tableHeaderView;
    newview.frame=CGRectMake(0, 0, DEVW, 410+ _webbackH.constant);
    [_tbv setTableHeaderView:newview];
    [_tbv reloadData];
}



- (IBAction)soucangBtna:(id)sender {
//    _soucangBtno.selected=!_soucangBtno.selected;
    [FuncPublic getData:self parameters:@{@"job_id":_Xid} RequestUrl:_soucangBtno.selected?cancleCollect:XaddCollect success:^(NSDictionary *acountInfo) {
     [WSProgressHUD showImage:nil status:@"收藏成功"];
        [self loaddat];
    }];

    
}
- (IBAction)shareBtna:(id)sender {
    [MyUtils shareWindowOpen:@{@"title":@"我在职贝勒发现一个好职位也许适合你,快去看看吧",@"share_url":share_url,@"name":_lb1.text,@"company":_lb11.text}];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  dataary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    HomeTbvCell2*cell=[HomeTbvCell2 CellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary*dic=dataary[indexPath.row];
    [cell Cellinit:dic];
    return cell;

}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 137;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    _Xid=WXGStr(dataary[indexPath.row][@"id"]);
    [self loaddat];
    [tableView  scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}

- (IBAction)Btna:(UIButton *)sender {
    [FuncPublic getData:self parameters:@{@"job_id":_Xid} RequestUrl:Cdeliver_resume success:^(NSDictionary *acountInfo) {
        
      [WSProgressHUD showImage:nil status:acountInfo[@"msg"]];

    }];

}

- (IBAction)CompanyBtna:(UIButton *)sender {
     [MyUtils collviewSelidex:@"公司详情" toVc:self andShopInfo:jcompanydic];
    
}


@end
